<template>
<div style="margin-left: 20px;margin-right: 20px;margin-top: 20px">
  <!--标题-->
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>下载表格</span>
    </div>
      <!--可下载内容Table-->
      <el-table
          :data="downloadData"
          border
          style="width: 100%">
        <el-table-column
            prop="filename"
            label="文件名称">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                type="primary"
                @click="down(scope.row)">下载</el-button>
          </template>
        </el-table-column>
      </el-table>
  </el-card>
</div>
</template>

<script>
import axios from "axios";
import { getFileListApi } from "@/api/GuideApi";

export default {
  name: "Download",
  data() {
    return {
      downloadData:[]
    }
  },
  methods: {
    down(row) {
      let URL = row.dlUrl
      const config = {
        method: 'get',
        url: URL,
        headers: {
          'Content-Disposition': 'attachment'
        },
        responseType: 'blob'
      };
      axios(config).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        let pos = URL.lastIndexOf('/')
        let fileName = URL.substr(pos+1)
        link.setAttribute('download', fileName);
        document.body.appendChild(link);
        link.click();

      })

    },
    getFileList(){
      getFileListApi().then(res => {
        if(res.code === 200) {
          this.downloadData = res.data
        }
      })
    }
  },
  created() {
    this.getFileList()
  }
}
</script>

<style scoped>

</style>
